// index page
// ============

define([ 'require', 'zepto', 'wx', 'dropwizard', 'sui'],
    function(require, $, wx, dropwizard) {
      return {
        init : function(data) {
          this.initList = [], 
          this.domArr = [];
          this.initPage();
          this.gotoDetail();
          this.initSearch();
        },
        
        initPage: function() {
          var that = this;
          $('.fita-card').each(function(a){
            $(this).attr('title' , '原位置为：'+(a+1));
            that.initList[a] = $(this).html();
          })
//          $('#drop').dropwizard()
//
//          $('#drop').on('shown:fancy:dropwizard', function(e) {
//            console.log(e)
//          })
//          
//          $('#drop').on('hidden:fancy:dropwizard', function(e) {
//            console.log("123")
//          })
          $('#openCloseEye').click(function() {
            $('.floatAgentPrice,.normalAgentPrice,.agentCommission').toggle();
            if ($('.floatAgentPrice,.normalAgentPrice,.agentCommission').css("display") == "inline") {
              $('.floatAgentPrice,.normalAgentPrice,.agentCommission').css("display", "block")
            }
            $('#openCloseEye').toggleClass('iconfont-openEye');
            $('#openCloseEye').toggleClass('iconfont-closedEye');
            
          });
          
        },
        
        initSearch: function(e) {
          var that = this;
          $(document).on("click", ".search-seat-button", function(e){
            e.preventDefault();
            $.closeModal(".search-seat-popup");
            that.doSearch();
          })
          
          $(document).on("click", ".search-category-button", function(e){
            e.preventDefault();
            $.closeModal(".search-category-popup");
            that.doSearch();
          })
          
          $(document).on("click", ".search-price-button", function(e){
            e.preventDefault();
            $.closeModal(".search-price-popup");
            that.doSearch();
          })
        },
        
        doSearch: function(e) {
          var that = this;
          var catrgories = [];
          var maxSeats;
          var minSeats;
          
          /*
          从小到大排序
           */
          function smallToBig(){
              getDom();
              $.each(that.domArr , function(i){
                  $.each(that.domArr , function(j){
                      if( parseInt(that.domArr[i].data("price")) < parseInt(that.domArr[j].data("price"))){
                          mid = that.domArr[j];
                          that.domArr[j] = that.domArr[i];
                          that.domArr[i] = mid;
                      }
                  })
              })
              appendDom();
          }
          
          /*
          取消排序
          */
         function cancleRank(){
             getDom();
             $.each(that.initList,function(b){
                 $.each(that.domArr,function(c){
                     if(that.domArr[c].html() == that.initList[b]){
                         $('.cars-list-layout').append(that.domArr[c]);
                     }
                 })
             })
         }
         
         /*
         从大到小排序
         */
          function bigToSmall(){
              getDom();
              $.each(that.domArr , function(i){
                  $.each(that.domArr , function(j){
                      if( parseInt(that.domArr[i].data("price")) > parseInt(that.domArr[j].data("price"))){
                          mid = that.domArr[j]; that.domArr[j] = that.domArr[i]; that.domArr[i] = mid;
                      }
                  })
              })
              appendDom()
          }
          
          function appendDom(){
            $.each(that.domArr, function(d){
                $('.cars-list-layout').append(that.domArr[d] );
            })
          }
          
          /*
          取出所有li放进数组
           */
          function getDom(){
              $('.fita-card').each(function( m ){
                that.domArr[m] = $(this);
              })
          }
          
          $("input[name='category']:checked").each(function(e) { 
            catrgories.push($(this).val());
          })
          
          var priceSort = $("input[name='price']:checked").val();
          var priceName = $("input[name='price']:checked").data("name");
          console.log(priceSort)
          if(priceSort) {
            $(".priceOrder").html("价格:"+priceName);
            
            if(priceSort == 'default') {
              cancleRank()
            } else if(priceSort == 'bigToHigh') {
              smallToBig()
            } else if(priceSort == 'highTobig') {
              bigToSmall();
            }
          }
          
          $("input[name='seat']:checked").each(function(e) {
            var values = $(this).val().split(",");
            minSeats = minSeats ? (minSeats > parseInt(values[0]) ? parseInt(values[0]) : minSeats) : parseInt(values[0]);
            maxSeats = maxSeats ? (maxSeats < parseInt(values[1]) ? parseInt(values[1]) : maxSeats) : parseInt(values[1]);
          })
          
          $(".fita-card").each(function(e) {
            $(this).addClass("hide");
          })
          
          if(catrgories.length > 0) {
           //选中车型
            var categoryName = $("input[name='category']:checked").data("name")
            var seatName = $("input[name='seat']:checked").parents(".item-inner").find(".item-title").html();
            $(".categoryName").html(categoryName)
            $(".seatName").html(seatName)
            if(catrgories.indexOf("0") >= 0 && ((minSeats == undefined && maxSeats == undefined) || (minSeats == 0 && maxSeats == 0 ))) {
              $(".seatName").html("全部座位")
              $(".categoryName").html("全部车型")
              $(".fita-card").removeClass("hide");
            } else {
              if((minSeats == undefined && maxSeats == undefined) || (minSeats == 0 && maxSeats == 0 )) {
                $(".seatName").html("全部座位")
              } else if(maxSeats == 100) {
                $(".seatName").html(minSeats+"座以上")
              } else {
                $(".seatName").html(minSeats+"-"+maxSeats+"座")
              }
              $(".fita-card").each(function(e) {
                var category = $(this).data("category");
                var seat = $(this).data("seat");
                var price = $(this).data("price");
                for(var i in catrgories) {
                  if(catrgories[i] ==category) {
                    if(((minSeats == undefined && maxSeats == undefined) || (minSeats == 0 && maxSeats == 0 ))) {
                      $(this).removeClass("hide");
                    }
                    if(seat>=minSeats && seat < maxSeats) {
                      $(this).removeClass("hide");
                    }
                    
                  }
                }
              })
            }
            
          } else {
            //没有选中车型
            if(minSeats != undefined & maxSeats != undefined && minSeats > 0 && maxSeats > 0 ) {
              $(".categoryName").html("全部车型")
              if(maxSeats == 100) {
                $(".seatName").html(minSeats+"座以上")
              } else {
                $(".seatName").html(minSeats+"-"+maxSeats+"座")
              }
              
              $(".fita-card").each(function(e) {
                var category = $(this).data("category");
                var seat = $(this).data("seat");
                var price = $(this).data("price");
                if(seat>=minSeats && seat <= maxSeats) {
                  $(this).removeClass("hide");
                }
              })
            } else {
              $(".seatName").html("全部座位")
              $(".categoryName").html("全部车型")
              $(".fita-card").removeClass("hide");
            }
          }
          
          
//          if(catrgories.length > 0) {
//            var categoryName = $("input[name='category']:checked").data("name")
//            $(".categoryName").html(categoryName)
//            $(".fita-card").each(function(e) {
//              var category = $(this).data("category");
//              var seat = $(this).data("seat");
//              var price = $(this).data("price");
//              for(var i in catrgories) {
//                if(catrgories[i] ==category) {
//                  if(minSeats != undefined & maxSeats != undefined) {
//                    if(maxSeats == 100) {
//                      $(".seatName").html(minSeats+"座以上")
//                    } else {
//                      $(".seatName").html(minSeats+"-"+maxSeats+"座")
//                    }
//                    if(seat>=minSeats && seat < maxSeats) {
//                      $(this).removeClass("hide");
//                    }
//                  } else {
//                    $(".seatName").html("全部座位")
//                    $(this).removeClass("hide");
//                  }
//                } else {
//                if(seat>=minSeats && seat < maxSeats) {
//                  $(this).removeClass("hide");
//                 }
//                  $(".categoryName").html("全部车型")
//                  $(".fita-card").removeClass("hide");
//                }
//              }
//            })
//          } else {
//            $(".categoryName").html("全部车型")
//            $(".fita-card").each(function(e) {
//              var category = $(this).data("category");
//              var seat = $(this).data("seat");
//              var price = $(this).data("price");
//              if(minSeats != undefined & maxSeats != undefined) {
//                if(seat>=minSeats && seat < maxSeats) {
//                  if(maxSeats == 100) {
//                    $(".seatName").html(minSeats+"座以上")
//                  } else {
//                    $(".seatName").html(minSeats+"-"+maxSeats+"座")
//                  }
//                  $(this).removeClass("hide");
//                }
//              } else {
//                $(".seatName").html("全部座位")
//                $(this).removeClass("hide");
//              }
//              
//            })
//          }
          
          if(minSeats != undefined & maxSeats != undefined) {
            $(".fita-card").each(function(e) {
              if(!$(this).hasClass("hide")) {
                var seat = $(this).data("seat");
                if(seat<minSeats || seat > maxSeats) {
                  
                }
              }
            })
          }
        
        },
        
        gotoDetail: function(e) {
          $(".detail-link").on("click", function(e){
            e.preventDefault();
            
            var ccode = $(this).data("ccode");
            var sizecode = $(this).data("sizecode");
            var dc = $(this).data("dc");
            var ac = $(this).data("ac");
            var ttype = $(this).data("ttype");
            var modelname = $(this).data("modelname");
            var modelcode = $(this).data("modelcode");
            var pic = $(this).data("pic");
            var pq = $(this).data("pq");
            var bq = $(this).data("bq");
            var ta = $(this).data("ta");
            var eta = $(this).data("eta");
            var ra = $(this).data("ra");
            var cc = $(this).data("cc");
            var setid = $(this).data("setid");
            var includeFees = $(this).data("includefees");
            var unIncludeFees = $(this).data("unincludefees");
            var includeOrderFee = $(this).data("includeorderfee");
            var unIncludeOrderFee = $(this).data("unincludeorderfee");
            var rateQualifier = $(this).data("ratequalifier");
            var vehicleCharge = $(this).data("vehiclecharge");
            var vehicleChargeId = $(this).data("vehiclechargeid");
            
            var href= $(this).data("href");
            
            $("input[name='categoryCode']").val(ccode);
            $("input[name='setId']").val(setid);
            $("input[name='sizeCode']").val(sizecode);
            var doorCount = !dc ? "0" : dc;
            $("input[name='doorCount']").val(doorCount);
            $("input[name='airConditionIn']").val(ac);
            $("input[name='transmissionType']").val(ttype);
            $("input[name='modelName']").val(modelname);
            $("input[name='modelCode']").val(modelcode);
            $("input[name='picture']").val(pic);
            $("input[name='passengerQuantity']").val(pq);
            $("input[name='baggageQuantity']").val(bq);
            $("input[name='totalAmount']").val(ta);
            $("input[name='esimatedTotalAmount']").val(eta);
            $("input[name='retailAmount']").val(ra);
            $("input[name='currencyCode']").val(cc);
            $("input[name='rateQualifier']").val(rateQualifier);
            
            if(includeFees) {
            	$("input[name='feeInclude']").val(includeFees);
            }
            if(unIncludeFees) {
            	$("input[name='feeUnInclude']").val(unIncludeFees);
            }
            if(includeOrderFee) {
            	$("input[name='includeOrderFeeId']").val(includeOrderFee);
            }
            
            if(unIncludeOrderFee) {
            	$("input[name='unIncludeOrderFeeId']").val(unIncludeOrderFee);
            }
            if (vehicleCharge) {
            	$("input[name='vehicleCharge']").val(vehicleCharge);
            	$("input[name='vehicleChargeId']").val(vehicleChargeId);
			}
            
            $.toast("正在查找车辆详细信息")
            
            var $form = $("#detailform");
            $form.attr("action", href);
            $form.submit();
          })
        }
      }
    })